#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
        WaitforElementByName(driver,u"管理",30).click()
        WaitforElementByName(driver,u"下载管理",30).click()
    except NoSuchElementException:
        raise NoSuchElementException

    try:
        button = WaitforElementById(driver,"com.baidu.appsearch:id/title_action2",5)
    except NoSuchElementException:
        driver.back()
        WaitforElementByName(driver,u"推荐",5).click()
        return
    else:
        print button.get_attribute("name")
        if button.get_attribute("name")==u'编辑':
            button.click()
    while True:
        try:
            ss = WaitforElementsById(driver,"com.baidu.appsearch:id/deletebtn",5)
        except NoSuchElementException:
            driver.back()
            WaitforElementByName(driver,u"推荐",5).click()
            break
        else:
            ss[0].click()
            WaitforElementById(driver,"com.baidu.appsearch:id/libui_cancel",5).click()
            time.sleep(2)
    driver.back()
    WaitforElementByName(driver,u"推荐",5).click()

def clickappdown(driver):
    try:
        all_btn = WaitforElementByName(driver,u"全部",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        all_btn.click()
    try:
        appitems = WaitforElementsById(driver,"com.baidu.appsearch:id/app_item",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for appitem in appitems:
            try:
                size=WaitforElementById(appitem,"com.baidu.appsearch:id/app_size",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[2:-1])<20:
                    continue
            appitem.click()
            try:
                down_btn = WaitforElementById(driver,"com.baidu.appsearch:id/app_detail_download_btn",30)
            except NoSuchElementException:
                raise NoSuchElementException
            else:
                down_btn.click()
                waittime=60
                while waittime:
                    try:
                        text = WaitforElementById(driver,"com.baidu.appsearch:id/app_content_btn_control_text",30).get_attribute("name")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0.0%' not in text:
                        down_btn.click()
                        driver.back()
                        break

                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            driver.back()
                            break
                        waittime=waittime-1
                    print "----------------------------------------------"



def game(driver):
    driver.find_element_by_name("分类").click()    # 点击元素
    driver.find_element_by_name("游戏").click()
    gamecatetory= [u'休闲益智',u'动作射击',u'体育竞速',u'单机游戏',u'网络游戏',u'棋牌桌游',u'经营策略',u'角色扮演',u'模拟辅助']
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,1)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def soft(driver):
    driver.find_element_by_name("软件").click()
    driver.find_element_by_name("分类").click()    # 点击元素
    gamecatetory= [u'社交通讯',u'影音播放',u'实用工具',u'金融理财',u'网上购物',u'手机管理',u'拍摄美化',u'旅游出行',u'资讯阅读',u'娱乐八卦',u'手机美化',u'效率办公',u'教育学习',u'生活服务',u'母婴育儿',u'医疗健康']
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k
        try:
            btn = WaitforElementByName(driver,k,1)
        except NoSuchElementException:
            continue
        else:
            btn.click()
            try:
                WaitforElementByName(driver,u'全部',30).click()
            except NoSuchElementException:
                driver.back()
                continue
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()
def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION            # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE        # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.baidu.appsearch'    # apk的包名
    desired_caps['appActivity'] = '.LauncherActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    driver.implicitly_wait(30)
    del_appdown_task(driver)
    soft(driver)
    game(driver)
    driver.quit()

#run()
